package com.example.demo01.demo;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.demo01.R;
import com.example.demo01.adapter.SpinnerDemoAdapter;
import com.example.demo01.consts.UserData;
import com.example.demo01.entity.UserDemo;
import com.example.demo01.fragments.TitleFragment;
import com.example.demo01.utils.ToastUtil;

public class SpinnerDemo extends AppCompatActivity {
    private ToastUtil toastUtil = new ToastUtil(this);
    private Spinner s_spinner, s_spinner_diy;

    private Boolean s_spinner_bool = false, s_spinner_diy_bool = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner);
        EdgeToEdge.enable(this);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        //. 添加标题
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.fl_title, TitleFragment.newInstance(getResources().getString(R.string.SpinnerDemo)))
                .commit();

        initView(); /// 初始化控件
    }

    /// 初始化View
    private void initView() {
        //. 获取 Spinner 控件
        s_spinner = findViewById(R.id.s_spinner);
        //. 获取自定义样式的Spinner控件
        s_spinner_diy = findViewById(R.id.s_spinner_diy);


        //. 为View添加事件
        addListeners();
    }

    //* 为View添加事件
    private void addListeners() {

        //* 给默认样式的Spinner设置选中事件监听
        s_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (s_spinner_bool) {
                    // 获取选中项的值
                    String selectedItem = parent.getSelectedItem().toString();
                    toastUtil.showToast(selectedItem);

                }
                s_spinner_bool = true;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // 无选中时的回调
            }
        });


        //* 新建SpinnerDemoAdapter适配器给自定义样式的Spinner
        SpinnerDemoAdapter spinnerDemoAdapter = new SpinnerDemoAdapter(this, UserData.USER_DEMOS);
        s_spinner_diy.setAdapter(spinnerDemoAdapter);
        s_spinner_diy.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (s_spinner_diy_bool) {
                    //* 获取选中项的值
                    UserDemo userDemo = spinnerDemoAdapter.getItem(position);
                    toastUtil.showToast(userDemo.getName());
                }
                s_spinner_diy_bool = true;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }
}
